package com.example.science.consumer;

import com.example.science.util.RedisKey;
import com.example.science.util.RedisUtil;
import com.example.science.util.SMSUtil;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

@Component//注入到spring容器中
@RabbitListener(queues = "send_code_phone_queue")//也可以监听多个队列
public class RegisterCodeConsumer {
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private SMSUtil smsUtil;
    @RabbitHandler
    public void handler(Map<String ,String> map)throws Exception{
        Integer code = smsUtil.sendCode(map.get("mobile"));
        //将验证码存入Redis中
        redisUtil.setString(RedisKey.registerCodeKey(map.get("mobile")),String.valueOf(code),300);
        //判断key是否存在
        if(redisUtil.exists(map.get("key"))){
            //让它自增
            redisUtil.increment(map.get("key"));
        }else{
            //新增一个key，过期时间为24小时
            redisUtil.setString(map.get("key"), "1", 86400);//86400秒等于24小时
        }
    }
}
